package com.web.systemParam.controller;

import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.enums.BaseStatus;
import com.web.common.enums.PendingAction;
import com.web.pendingList.service.PendingListService;
import com.web.systemParam.entity.SystemParam;
import com.web.systemParam.service.SystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-01-09 2024/1/9
 */

@RestController
@CrossOrigin
@RequestMapping("/systemParam")
public class SystemParamController {

    @Autowired
    private SystemParamService systemParamService;

    @Autowired
    private PendingListService pendingService;

    @RequestMapping(value = "/systemParamList")
    public BaseResult<List<SystemParam>> getSystem(@ModelAttribute SystemParam systemParam){

        List<SystemParam> systemParamList = systemParamService.getSystemParamList(systemParam.getSystemParamName(), systemParam.getSystemParamType());
        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),systemParamList);
    }


    @PostMapping(value = "/addSystemParam")
    public BaseResult addSystem(@ModelAttribute SystemParam systemParam,String userName){
        //校验数据是否已经存在.
        if(systemParamService.checkDuplicateData(systemParam.getSystemParamName()) != null){
            return new BaseResult<>(BaseStatus.SYSTEM_PARAM_ALREADY_EXIST.getCode(),BaseStatus.SYSTEM_PARAM_ALREADY_EXIST.getDesc());
        }

        //新增相关的Pending记录和SystemParam数据
         return  pendingService.addSystemParamPending(systemParam,userName,SystemParam.MODULE, PendingAction.ADD.getAction());
    };

    @PostMapping(value = "/updateSystemParam")
    public BaseResult updateSystem(@ModelAttribute SystemParam systemParam,String userName){
        //查询出还未修改的systemParam数据.
        List<SystemParam> systemParamList = systemParamService.getSystemParamList(systemParam.getSystemParamName(), systemParam.getSystemParamType());
        if(systemParamList.isEmpty()){
            return new BaseResult<>(BaseStatus.SYSTEM_PARAM_DATA_NOT_EXIST.getCode(),BaseStatus.SYSTEM_PARAM_DATA_NOT_EXIST.getDesc());
        }

        SystemParam systemParamOld = systemParamList.get(0);

        //处理审批和systemParam数据.
        pendingService.handleModifySystemParam(systemParamOld,systemParam,userName);

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),BaseStatus.FCE_UPDATE_SUCCESS.getDesc());

    };

    @PostMapping(value = "/deleteSystemParam")
    public BaseResult deleteSystem(@RequestParam("systemParamName") String systemParamName,String userName){
        //根据参数名称查询出需要删除的系统参数
        List<SystemParam> systemParamList = systemParamService.getSystemParamList(systemParamName, null);
        if(systemParamList.isEmpty()){
            return new BaseResult<>(BaseStatus.SYSTEM_PARAM_DATA_NOT_EXIST.getCode(),BaseStatus.SYSTEM_PARAM_DATA_NOT_EXIST.getDesc());
        }

        //新增相关的Pending记录以及SystemParam数据修改
        return pendingService.addSystemParamPending(systemParamList.get(0),userName,SystemParam.MODULE,PendingAction.DEL.getAction());
    };
}
